//
//  ZLJTabViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/8.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "ZLJTabViewController.h"

@interface ZLJTabViewController ()

@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) NSArray *normalImages;
@property (nonatomic, strong) NSArray *selectedImages;

@property (nonatomic, strong) UIView *viewContainer;
@property (nonatomic, strong) UIView *buttonContainer;

@end
enum {
    ButtonTag = 100,
};

@implementation ZLJTabViewController
#pragma mark -- initialize
- (instancetype)initWithViewControllers:(NSArray *)viewControllers normalImages:(NSArray *)normalImages selectedImages:(NSArray *)selectedImages {
    self = [super init];
    if (!self) {
        self.viewControllers = viewControllers;
        self.normalImages = normalImages;
        self.selectedImages = selectedImages;
    }
    return self;
}
#pragma mark -- lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearabce];
    
}
#pragma mark - private methods
- (void)initializeAppearabce {
    //视图数组 按钮数组
    [self.view addSubview:self.viewContainer];
    [self.view addSubview:self.buttonContainer];
    
    [self.viewControllers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //添加按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *normalImage = self.normalImages[idx];
        UIImage *selectedImage = self.selectedImages[idx];
        [button setImage:normalImage forState:UIControlStateNormal];
        [button setImage:selectedImage forState:UIControlStateSelected];
        CGFloat width = 375.f / self.viewControllers.count;
        CGFloat height = width / (normalImage.size.width / normalImage.size.height);
        button.frame = CGRectMake(width * idx, 0, width, height);
        [button addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = ButtonTag + idx;
        
        [self.buttonContainer addSubview:button];
    }];
    //初始化为第一个视图
    self.selectedIndex = 0;

}

#pragma mark - callback/action
- (void)respondsToButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.selectedIndex = sender.tag - ButtonTag;
}

#pragma mark - setter
- (void)setSelectedIndex:(NSInteger)selectedIndex {
    //移除当前显示的ViewController
    UIViewController *currentViewController = self.viewControllers[_selectedIndex];
    if (currentViewController.parentViewController) {
        [currentViewController.view removeFromSuperview];
        [currentViewController removeFromParentViewController];
        [currentViewController willMoveToParentViewController:nil];
    }
    //把之前选中的按钮的selected设为NO
    UIButton *currentButton = (UIButton *)[self.buttonContainer viewWithTag:ButtonTag + _selectedIndex];
    currentButton.selected = NO;
    
    _selectedIndex = selectedIndex;
    UIViewController * controller = self.viewControllers[selectedIndex];
    //关联 controller
    [self addChildViewController:controller];
     //添加视图层级
    [self.viewContainer addSubview:controller.view];
    [controller didMoveToParentViewController:self];
    
    //把即将选中的按钮的selected设为YES
    UIButton *button = (UIButton *)[self.buttonContainer viewWithTag:ButtonTag + _selectedIndex];
    button.selected = YES;
}

#pragma mark - getter
- (UIView *)viewContainer {
    if (!_viewContainer) {
        _viewContainer = [[UIView alloc] initWithFrame:self.view.bounds];
    }
    return _viewContainer;
}

- (UIView *)buttonContainer {
    if (!_buttonContainer) {
        CGFloat height = 67 / 1.28;
        _buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 667 - height, 375.f, height)];
    }
    return _buttonContainer;
}



@end
